<?php
session_start();
//Sanity check => Check if logged in -> if not die
if($_SESSION['valid_login'] == false)
{
	die();
}

require_once('dataManager/definitions.php');
require_once('dataManager/DataManager.php');

$uid = $_SESSION['uid'];
$dbM = new DataManager(DB_USER, DB_PASSWORD, "localhost", NB_DATABASE_ADMIN);
$dbM_link = new DataManager(DB_USER, DB_PASSWORD, "localhost", NB_DATABASE_ADMIN);
$dbM->selectAllFromTable(NB_ADMIN_TABLE_APPLICATIONS, NB_ADMIN_APPLICATIONS_COLUMN_OWNERID."=$uid");  
$numApps = $dbM->retrieveNumberOfResults();

echo("
<div id=\"titlePageBox\">Manage Data</div>

<div class=\"subTitle\">Applications - $numApps</div>
<table id=\"applications\">
    <tr>
        <td class=\"tdB\">Appid</td>
        <td class=\"tdB\">Title</td>
        <td class=\"tdB\">Access</td>
        <td class=\"tdB\">Chart type</td>
        <td class=\"tdB\">Number of streams</td>
        <td class=\"tdB\">Modify</td>
    </tr>
");
    $i = 1;
    while($app = $dbM->retrieveResult())
    {
        if($app[NB_ADMIN_APPLICATIONS_COLUMN_SHARED])
            $access = "Public";
        else
            $access = "Private";
        
        $appid    = $app[NB_ADMIN_APPLICATIONS_COLUMN_APPID];
        $appTitle = $app[NB_ADMIN_APPLICATIONS_COLUMN_TITLE];
        
        if($app[NB_ADMIN_APPLICATIONS_COLUMN_CHARTTYPE])
            $chartType = "Column";
        else
            $chartType = "Line";
        
        $dbM_link->selectAllFromTable(NB_ADMIN_TABLE_APPSTREAMLINK, NB_ADMIN_APPSTREAMLINK_COLUMN_APPID."=$appid");  
        $numSids = $dbM_link->retrieveNumberOfResults();
        if ($i%2 == 1)
           echo "<tr name=\"$appid\">"; 
        else
            echo"<tr name=\"$appid\" class=\"rowShade\">";
        
        echo "
        <td>$appid</td>
        <td>$appTitle</td>
        <td>$access</td>
        <td>$chartType</td>
        <td>$numSids</td>
        <td><a href=\"?\" id=\"deleteApp\" class=\"manageAppLink\" appid=\"$appid\"><span style=\"color:red;\">X</span></a></td>
        </tr>";
        
        $i++;
    }
    echo"<tr class=\"createRow\">";
    echo "
    <td >-</td>
    <td><input type=\"text\" id=\"app_title\"\></td>
    <td>
        <select id=\"app_access\">
            <option value=\"0\">Private</option>
            <option value=\"1\">Public</option>
        </select>
    </td>
    <td>
        <select id=\"app_chartType\">
            <option value=\"0\">Line</option>
            <option value=\"1\">Column</option>
        </select>
    </td>
    <td> 
        <select id=\"app_sid_select\">";
            $dbM->selectAllFromTable(NB_ADMIN_TABLE_STREAMS, NB_ADMIN_STREAMS_COLUMN_OWNERUID."=$uid"." OR ".NB_ADMIN_STREAMS_COLUMN_PERMISSION."=0".
                                                             " ORDER BY ".NB_ADMIN_STREAMS_COLUMN_SID." ASC " );
            while($stream = $dbM->retrieveResult())
            {
                $s   = $stream[NB_ADMIN_STREAMS_COLUMN_SID];
                $tag = $stream[NB_ADMIN_STREAMS_COLUMN_TAG];
                echo "<option value=\"$s\">$s - $tag</option>";   
            }
            echo"
        </select>
        <div id=\"selectedIds\">
            
        </div>
    </td>
    <td><a href=\"?\" id=\"createApp\" class=\"manageAppLink\"><span style=\"color:green;\">Add</span></a></td>
    </tr>";
    
echo("
</table>

<div class=\"subTitle\">Streams</div>
--To be implemented--
");



?>